﻿using Microsoft.ExtendedReflection.Utilities;
using Microsoft.ExtendedReflection.Utilities.Safe;
using Microsoft.Win32;
using System;
using System.Diagnostics;
using System.IO;

namespace MolesVS2012.Host
{
    internal static class VsHostRegistration
    {
        private static readonly string[] testTypeGuids = new string[1]
    {
      "{13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b}"
    };

        static VsHostRegistration()
        {
        }

        public static void Register(string visualStudioVersion)
        {
            VsHostRegistration.Unregister(visualStudioVersion);
            Register(Registry.LocalMachine.OpenSubKey("SOFTWARE"), visualStudioVersion);
            Register(Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node"), visualStudioVersion);
        }

        private static void Register(RegistryKey key, string visualStudioVersion)
        {
            string name = SafeString.Format("Microsoft\\VisualStudio\\{0}", (object)visualStudioVersion);
            using (RegistryKey registryKey1 = key.OpenSubKey(name, true))
            {
                if (registryKey1 == null)
                    return;
                else
                {
                    string str1 = "EnterpriseTools\\QualityTools";
                    RegistryKey registryKey2;
                    using (registryKey2 = registryKey1.OpenSubKey(str1, true))
                    {
                        if (registryKey2 == null)
                            registryKey2 = registryKey1.CreateSubKey(str1);
                        string fullName = typeof(VsHostRegistration).Assembly.FullName;
                        RegistryKey registryKey3;
                        using (registryKey3 = registryKey2.OpenSubKey("HostAdapters", true))
                        {
                            if (registryKey3 == null)
                                registryKey3 = registryKey2.CreateSubKey("HostAdapters");
                            using (RegistryKey subKey1 = registryKey3.CreateSubKey("Moles"))
                            {
                                subKey1.SetValue("Type", (object)("MolesVS2012.Host.Agent.MolesAgentAdapter, " + fullName), RegistryValueKind.String);
                                subKey1.SetValue("EditorType", (object)("MolesVS2012.Host.Agent.MolesRunConfigurationCustomHostEditor, " + fullName), RegistryValueKind.String);
                                using (RegistryKey subKey2 = subKey1.CreateSubKey("SupportedTestTypes"))
                                {
                                    foreach (string name2 in VsHostRegistration.testTypeGuids)
                                        subKey2.SetValue(name2, (object)string.Empty, RegistryValueKind.String);
                                }
                            }
                        }
                        foreach (string str2 in VsHostRegistration.testTypeGuids)
                        {
                            string name2 = "TestTypes\\" + str2;
                            using (RegistryKey registryKey4 = registryKey2.OpenSubKey(name2))
                            {
                                if (registryKey4 != null)
                                {
                                    RegistryKey registryKey5;
                                    using (registryKey5 = registryKey4.OpenSubKey("SupportedHostAdapters", true))
                                    {
                                        if (registryKey5 == null)
                                            registryKey5 = registryKey4.CreateSubKey("SupportedHostAdapters", RegistryKeyPermissionCheck.ReadWriteSubTree);
                                        registryKey5.SetValue("Moles", (object)"Moles Host Adapter");
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        public static void Unregister(string visualStudioVersion)
        {
            Unregister(Registry.LocalMachine.OpenSubKey("SOFTWARE"), visualStudioVersion);
            Unregister(Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Wow6432Node"), visualStudioVersion);
        }

        private static void Unregister(RegistryKey key, string visualStudioVersion)
        {
            string name = SafeString.Format("Microsoft\\VisualStudio\\{0}\\EnterpriseTools\\QualityTools", (object)visualStudioVersion);
            using (RegistryKey registryKey1 = key.OpenSubKey(name, true))
            {
                if (registryKey1 == null)
                    return;
                using (RegistryKey registryKey2 = registryKey1.OpenSubKey("HostAdapters", true))
                {
                    if (registryKey2 != null)
                    {
                        if (SafeArray.Contains<string>(registryKey2.GetSubKeyNames(), "Moles"))
                            registryKey2.DeleteSubKeyTree("Moles");
                    }
                }
                foreach (string str in VsHostRegistration.testTypeGuids)
                {
                    using (RegistryKey registryKey2 = registryKey1.OpenSubKey("TestTypes\\" + str + "\\SupportedHostAdapters", true))
                    {
                        if (registryKey2 != null)
                        {
                            if (SafeArray.Contains<string>(registryKey2.GetValueNames(), "Moles"))
                                registryKey2.DeleteValue("Moles", false);
                        }
                    }
                }
            }
        }

        [Conditional("PRIVATE")]
        public static void GeneratePackageDefinition(string packageDefinitionFile)
        {
            string contents = SafeString.Format("Windows Registry Editor Version 5.00\r\n\r\n[$RootKey$\\EnterpriseTools\\QualityTools\\HostAdapters\\Moles]\r\n\"Type\"=\"MolesVS2012.Host.Agent.MolesAgentAdapter, MolesVS2012.Host, Version={0}, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"\r\n\"EditorType\"=\"MolesVS2012.Host.Agent.MolesRunConfigurationCustomHostEditor, MolesVS2012.Host, Version={0}, Culture=neutral, PublicKeyToken=31bf3856ad364e35\"\r\n\r\n[$RootKey$\\EnterpriseTools\\QualityTools\\HostAdapters\\Moles\\SupportedTestTypes]\r\n\"{{13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b}}\"=\"\"\r\n\"{{62d4df95-08c4-47f7-95ec-a3a74e304231}}\"=\"\"\r\n\r\n[$RootKey$\\EnterpriseTools\\QualityTools\\TestTypes\\{{13cdc9d9-ddb5-4fa4-a97d-d965ccfc6d4b}}\\SupportedHostAdapters]\r\n\"Moles\"=\"Moles Host Adapter\"\r\n\r\n", (object)typeof(VsHostRegistration).Assembly.GetName().Version);
            File.WriteAllText(packageDefinitionFile, contents);
        }
    }
}
